package com.beauty.dressing.module.mine.activity;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageButton;

import com.framework.common.base.IFragmentPagerAdapter;
import com.framework.common.view.tablayout.CommonTabLayout;
import com.framework.common.view.tablayout.bean.BaseTabEntity;
import com.framework.common.view.tablayout.listener.CustomTabEntity;
import com.framework.common.view.tablayout.listener.OnTabSelectListener;
import com.beauty.dressing.R;
import com.beauty.dressing.common.base.BaseFragment;
import com.beauty.dressing.common.base.BaseFragmentActivity;
import com.beauty.dressing.common.utils.Constants;
import com.beauty.dressing.common.view.NoScrollViewPager;
import com.beauty.dressing.module.mine.fragment.HistoricalLeaseFragment;
import com.beauty.dressing.module.mine.fragment.MyInRentFragment;
import com.beauty.dressing.module.mine.fragment.WithdrawalFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的租赁
 */
public class MyLeaseActivity extends BaseFragmentActivity implements OnTabSelectListener {
    public static final int TAG_INRENT = 0;
    public static final int TAG_WITHDRAWAL = 1;
    public static final int TAG_HISTORICALLEASE = 2;
    private int status;
    private long sales_id;
    private CommonTabLayout mTabLayout;
    public NoScrollViewPager mViewPager;
    private BaseFragment[] fragments;
    private ImageButton mBackImg;

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_my_lease);
    }

    @Override
    public void findView() {
        mTabLayout = (CommonTabLayout) findViewById(R.id.tab_layout);
        mViewPager = (NoScrollViewPager) findViewById(R.id.view_pager);
        mBackImg = (ImageButton) findViewById(R.id.left_img_btn);
        mBackImg.setOnClickListener(this);
        mTabLayout.setOnTabSelectListener(this);
        ArrayList<CustomTabEntity> list = new ArrayList<>();
        list.add(new BaseTabEntity("在租"));
        list.add(new BaseTabEntity("退租"));
        list.add(new BaseTabEntity("历史租赁"));
        mTabLayout.setTabData(list);
    }

    @Override
    public void initData() {
        fragments = new BaseFragment[3];
        fragments[TAG_INRENT] = new MyInRentFragment();
        fragments[TAG_WITHDRAWAL] = new WithdrawalFragment();
        fragments[TAG_HISTORICALLEASE] = new HistoricalLeaseFragment();
        List<BaseFragment> fragmentList = new ArrayList<>();
        fragmentList.add(fragments[TAG_INRENT]);
        fragmentList.add(fragments[TAG_WITHDRAWAL]);
        fragmentList.add(fragments[TAG_HISTORICALLEASE]);
        setFragmentsParam();
        mViewPager.setOffscreenPageLimit(3);
        IFragmentPagerAdapter mPagerAdapter = new IFragmentPagerAdapter(getSupportFragmentManager());
        mPagerAdapter.setList(fragmentList);
        mViewPager.setAdapter(mPagerAdapter);

        if (getIntent().getIntExtra("Status", -1) != -1) {
            status = getIntent().getIntExtra("Status", -1);
            mViewPager.setCurrentItem(status);
            mTabLayout.setCurrentTab(status);
        }
        mViewPager.setOffscreenPageLimit(fragmentList.size());
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position != mTabLayout.getCurrentTab()) {
                    mTabLayout.setCurrentTab(position);
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }

    private void setFragmentsParam() {
        for (int i = 0; i < fragments.length; i++) {
            Bundle argment = new Bundle();
            argment.putLong(Constants.SALES_ID, sales_id);
            fragments[i].setArguments(argment);
        }
    }

    @Override
    public void onTabSelect(int position) {
        if (position != mViewPager.getCurrentItem()) {
            mViewPager.setCurrentItem(position, true);
        }
    }

    @Override
    public void onTabReselect(int position) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.left_img_btn:
                finish();
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        fragments[TAG_INRENT].onDestroyView();
        fragments[TAG_WITHDRAWAL].onDestroyView();
        fragments[TAG_HISTORICALLEASE].onDestroyView();
    }
}
